//
//  ZCConcaveView.m
//  ZhaoCaiMao
//
//  Created by wtjr on 2017/3/14.
//  Copyright © 2017年 谢英泽. All rights reserved.
//

#import "ConcaveView.h"
#import "AppHeader.h"

@interface ConcaveView ()

@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@end

@implementation ConcaveView

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //高度为20
    //画曲线
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    self.shapeLayer = shapeLayer;
    //设置虚线颜色
    [shapeLayer setStrokeColor:kCOLOR_RGBValue(0xdcd2de).CGColor];
    
    //设置虚线的宽度
    [shapeLayer setLineWidth:0.5f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    //设置宽度、间距  5=线的宽度 4=每条线的间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:4],nil]];
    
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 15, 10);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(self.frame)-15,10);
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    //添加到UIView上
    [self.layer addSublayer:shapeLayer];
    
    //添加半圆
    CAShapeLayer *leftLayer = [self semicircleLayerWithStartAngle:M_PI*3/2 endAngle:M_PI/2 andCenter:CGPointMake(0, 10)];
    [self.layer addSublayer:leftLayer];
    
    CAShapeLayer *rightLayer = [self semicircleLayerWithStartAngle:M_PI/2 endAngle:M_PI*3/2 andCenter:CGPointMake(CGRectGetWidth(self.frame), 10)];
    [self.layer addSublayer:rightLayer];
}

//画半圆
- (CAShapeLayer *)semicircleLayerWithStartAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle andCenter:(CGPoint)center {
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:10 startAngle:startAngle endAngle:endAngle clockwise:YES];
    layer.fillColor = kCOLOR_RGBValue(0xf1f1f1).CGColor;
    layer.strokeColor = [UIColor clearColor].CGColor;
    layer.path = path.CGPath;
    return layer;
}

@end
